package org.example.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.example.api.CommonResult;
import org.example.model.Customer;
import org.example.model.HairType;
import org.example.service.CustomerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@Api(tags = "CustomerController", description = "会员管理")
@RestController
@RequestMapping(value = "/customer")
public class CustomerController {
    private static final Logger LOGGER = LoggerFactory.getLogger(CustomerController.class);
    @Autowired
    private CustomerService customerService;

    @ApiOperation("获取所有会员信息")
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<Customer>> listAll() {
        return CommonResult.success(customerService.listAll());
    }

    @ApiOperation("获取单个会员信息")
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Customer> getCustomerById(@PathVariable("id") @ApiParam("id查询") Long id) {
        return CommonResult.success(customerService.getById(id));
    }


    @ApiOperation("添加会员信息")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody Customer customer) {
        CommonResult commonResult;
        int count = customerService.create(customer);
        if (count == 1) {
            commonResult = CommonResult.success(customer);
            LOGGER.debug("createCustomer success:{}", customer);
        } else {
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("createCustomer failed:{}", customer);
        }
        return commonResult;
    }

    @ApiOperation("更新会员信息")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable("id") Long id, @RequestBody Customer customer) {
        CommonResult commonResult;
        int count = customerService.update(id, customer);
        if (count == 1) {
            commonResult = CommonResult.success(customer);
            LOGGER.debug("updateCustomer success:{}", customer);
        } else {
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("updateCustomer failed:{}", customer);
        }
        return commonResult;
    }

    @ApiOperation("删除会员信息")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public CommonResult delete(@PathVariable("id") Long id) {
        int count = customerService.delete(id);
        if (count == 1) {
            LOGGER.debug("deleteCustomer success :id={}", id);
            return CommonResult.success(null);
        } else {
            LOGGER.debug("", id);
            return CommonResult.failed("操作失败");
        }
    }

    @ApiOperation("通过电话获取单个会员信息")
    @RequestMapping(value = "/condition/{condition}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<Customer>> getCustomerByCondition(@PathVariable("condition") @ApiParam("电话查询") int condition) {
        return CommonResult.success(customerService.getByCondition(condition));
    }

    @RequestMapping(value = "/consume/{id}/{selectId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult consume(@PathVariable("id") Long id, @PathVariable("selectId") Long selectId) {
        return CommonResult.success(customerService.consume(id, selectId));
    }

    @RequestMapping(value = "/recharge/{id}/{amount}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult recharge(@PathVariable("id") Long id, @PathVariable("amount") BigDecimal amount) {
        return CommonResult.success(customerService.recharge(id, amount));
    }
}
